package com.qiwx.pigeon.fragment;

import android.content.Intent;
import android.os.Build;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;

import com.facebook.drawee.view.SimpleDraweeView;
import com.jzg.lib.crash.LogsActivity;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DBSqlUtil;
import com.qiwx.pigeon.Utils.SharedPreferencesUtil;
import com.qiwx.pigeon.activity.account.LoginActivity;
import com.qiwx.pigeon.activity.account.ModifyPwdActivity;
import com.qiwx.pigeon.activity.account.UserManagerActivity;
import com.qiwx.pigeon.activity.stock.StockManagerActivity;
import com.qiwx.pigeon.activity.manager.WareHouseManagerActivity;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseFragment;
import com.qiwx.pigeon.data.RoleConfig;
import com.qiwx.pigeon.data.vo.EmployBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.helper.DialogHelper;
import com.qiwx.pigeon.widget.ArrowView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-03-23 14:00
 * 我的
 **/
public class MineFragment extends BaseFragment {
    @BindView(R.id.img)
    SimpleDraweeView img;
    @BindView(R.id.tvName)
    TextView tvName;
    @BindView(R.id.tvCompany)
    TextView tvCompany;
    @BindView(R.id.tvRole)
    TextView tvRole;
    @BindView(R.id.rlUserInfo)
    RelativeLayout rlUserInfo;
    @BindView(R.id.rel_warehouse_manager)
    RelativeLayout relWarehouseManager;
    @BindView(R.id.rel_modify_pwd)
    RelativeLayout relModifyPwd;
    @BindView(R.id.rel_user_manager)
    RelativeLayout relUserManager;
    @BindView(R.id.btn_exit)
    TextView btnExit;
    @BindView(R.id.btn_setting)
    Button btnSetting;
    List<String> ids = new ArrayList<>();
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.title_return)
    ArrowView titleReturn;
    @BindView(R.id.rel_log)
    RelativeLayout relLog;
    EmployBean employBean;
    @BindView(R.id.rel_stock_manager)
    RelativeLayout relStockManager;


    @Override
    public int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public void initAfterOnCreate() {
        titleReturn.setVisibility(View.GONE);
        titleContent.setText("个人中心");
    }

    @Override
    public void onResume() {
        super.onResume();
        employBean = SharedPreferencesUtil.getEmployBean(getContext());
        if (employBean != null) {
            tvName.setText(employBean.getLogin_name());
            tvRole.setText(RoleConfig.getRoleDes(employBean.getRole()));
            tvCompany.setText(employBean.getName());
            if (employBean.isManager()) {
                relWarehouseManager.setVisibility(View.VISIBLE);
                relUserManager.setVisibility(View.VISIBLE);
               // relStockManager.setVisibility(View.VISIBLE);
            } else {
                relWarehouseManager.setVisibility(View.GONE);
                relUserManager.setVisibility(View.GONE);
                //relStockManager.setVisibility(View.GONE);

            }
        }


    }

    private void exit() {
        SharedPreferencesUtil.removeEmployBean(getActivity());
        AppContext.houses.clear();
        AppContext.employBean = null;
        startActivity(new Intent(getActivity(), LoginActivity.class));
        if (getActivity() != null) {
            getActivity().finish();
        }
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @OnClick({R.id.rel_user_manager, R.id.rel_warehouse_manager, R.id.btn_exit, R.id.btn_setting, R.id.rel_modify_pwd, R.id.rel_log})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rel_user_manager://下属账号管理
                startActivity(new Intent(getActivity(), UserManagerActivity.class));
                break;
            case R.id.rel_warehouse_manager:
                startActivity(new Intent(getActivity(), WareHouseManagerActivity.class));
                break;
            case R.id.rel_stock_manager:
                startActivity(new Intent(getActivity(), StockManagerActivity.class));
                break;
            case R.id.btn_exit:
                AlertDialog.Builder builder = new AlertDialog
                        .Builder(getActivity())
                        .setTitle("确定要退出么?")
                        .setPositiveButton("确定", ((dialog, which) -> {
                            exit();
                        })).setNegativeButton("取消", ((dialog, which) -> {

                        }));
                builder.show();

                break;
//            case R.id.btn_setting:
//                startActivity(new Intent(getActivity(), SettingActivity.class));
//                break;
            case R.id.rel_modify_pwd:
                startActivity(new Intent(getActivity(), ModifyPwdActivity.class));
                break;
            case R.id.rel_log:
                startActivity(new Intent(getActivity(), LogsActivity.class));
                break;

        }
    }


    private void queryWareHouse() {
        ids.clear();
        List<WareHouseBean> result = DBSqlUtil.queryWareHouse();
        for (int i = 0; i < result.size(); i++) {
            ids.add(result.get(i).getName());
        }
    }


    public void selectWareHouse() {
        queryWareHouse();
        String[] items = ids.toArray(new String[ids.size()]);
        DialogHelper.showSelectWareHouseDialog(getActivity(), items);

    }
}
